<?php
namespace app\web\admin;

use app\common\builder\ZBuilder;
use app\web\model\Category as MyModel;


/**
 * 轮播管理（后台）
 * @package
 */
class Category extends Base 
{
	public function index()
	{
		$map = $this->getMap();

		$data_list = MyModel::where($map)
					->order($this->getOrder("sort desc,id desc"))
					->paginate();
		return ZBuilder::make('table')
			->setTableName('category')
			->addColumns([
				['category_name','分类名称'],
				['image_id','分类图标','picture'],
				['position_name','服务部位'],
				['sort','排序','text.edit'],
				['status','状态','switch'],
				['right_button', '操作', 'btn']
			])
			->setRowList($data_list)
			->setSearch(['category_name' => '分类名称']) // 设置搜索参数
			->addOrder('sort,cateory_time')
			->addFilterList('status',['禁用','启用'])
			->addValidate('Sort', 'sort')
			->addTopButtons(['add','disable','enable','delete'=>['data-tips' => '删除了就无法恢复了']])
			->addRightButtons(['edit','delete'=>['data-tips' => '删除了就无法恢复了']])
			->fetch();
	}

	public function add(){
		# 保存数据
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$result = $this->validate($data,'Category');
			if($result !== true) $this->error($result);
			$data['image'] = $this->setImgPath($data['image_id']);
			if (MyModel::create($data)) {
				$this->success('新增成功','index');
			} else {
				$this->error('新增失败');
			}
		}
		#获取数据，渲染模板
		return ZBuilder::make('form')
			->addFormItems([
				['text', 'category_name', '分类名称', '必填'],
			    ['image', 'image_id', '分类图标', '必传,建议尺寸78*78'],
			    ['tags','position_name','服务部位','必填'],
			    ['radio','is_face','接单是否刷脸认证','',['否','是'],0],
			    ['number', 'sort', '排序', '必填',100],
			    ['radio','project_technician','优先展示','', ['项目','技师'],0],
			    ['radio', 'status', '状态', '', ['禁用', '启用'], 1],
			])
			->fetch();
	}
	public function edit($id = null){
		$info = MyModel::get($id);
		# 保存数据
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$result = $this->validate($data,'Category');
			if($result !== true) $this->error($result);
			$data['image'] = $this->setImgPath($data['image_id']);
			if ($info->save($data)) {
				$this->success('编辑成功','index');
			} else {
				$this->error('编辑失败');
			}
		}

		#获取数据，渲染模板
		return ZBuilder::make('form')
			->addFormItems([
				['hidden','id'],
				['hidden','category_type'],
				['text', 'category_name', '分类名称', '必填'],
			    ['image', 'image_id', '分类图标', '必传,建议尺寸78*78'],
			    ['tags','position_name','服务部位','必填'],
			    ['radio','is_face','接单是否刷脸认证','',['否','是'],0],
			    ['number', 'sort', '排序', '必填',100],
			    ['radio','project_technician','优先展示','',['项目','技师'],0],
			    ['radio', 'status', '状态', '', ['禁用', '启用'], 1],
			])
			->setTrigger('category_type','0','position_name,is_face,project_technician')
			->setFormData($info)
			->fetch();
	}
	/**
	 * 删除
	 * [delete description]
	 * @param  array  $record [description]
	 * @return [type]         [description]
	 */
    public function delete($record = [])
    {
    	$ids   = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $ids   = (array)$ids;
        MyModel::destroy($ids);
        $this->success('操作成功');
    }
}